package com.spi.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.RequestPath;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Order(1) //@order表示过滤器权重，值越小优先级越高
@Component
public class AuthorizeFilter implements GlobalFilter {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //判断若是登录请求则下述校验均不走直接放行
        RequestPath path = exchange.getRequest().getPath();
        System.out.println("请求地址为：===" + path);
        //1:获取请求参数
        MultiValueMap<String, String> queryParams = exchange.getRequest().getQueryParams();
        //2：获取authorization参数
        String authorization = queryParams.getFirst("authorization");
        //3：校验
//        if ("admin".equals(authorization)){
//            //放行
//            return chain.filter(exchange);
//        }
//        //拦截 禁止访问 设置状态码
//        exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
//        return exchange.getResponse().setComplete();
        return chain.filter(exchange);
    }
}
